//
//  TravelViewController.m
//  NineGroupProject
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "TravelViewController.h"
#import "Masonry.h"
#import "ForeignViewController.h"
#import "InlandViewController.h"
#import "SearchViewController.h"
@interface TravelViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, strong) UISegmentedControl *segmentC;
@property (nonatomic, strong) ForeignViewController *foreignVC;
@property (nonatomic, strong) InlandViewController *inlandVC;
@end

@implementation TravelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    UIImage *rightImage = [UIImage imageNamed:@"iconfont-sousuo.png"];
    
    rightImage = [rightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:rightImage style:UIBarButtonItemStyleDone target:self action:@selector(rightButtonAction)];

    
    //navigationItem title
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectZero];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:@"旅游"];
    customLab.textAlignment = NSTextAlignmentCenter;
    customLab.font = [UIFont boldSystemFontOfSize:20];
    [customLab sizeToFit];
    self.navigationItem.titleView = customLab;
    
    //titleView 上放segment
    self.titleV = [[UIView alloc] init];
    self.titleV.layer.masksToBounds = YES;
    self.titleV.layer.cornerRadius = 10;
    [self.view addSubview:self.titleV];
    [self.titleV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.mas_equalTo(0);
        make.width.mas_equalTo(WIDTH);
        make.height.mas_equalTo(40 *HEI);
        
    }];
    
    //segment
    self.array = [NSArray arrayWithObjects:@"国外",@"国内", nil];
    self.segmentC = [[UISegmentedControl alloc] initWithItems:self.array];
    [self.titleV addSubview:self.segmentC];
    [self.segmentC mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10 * WID);
        make.top.mas_equalTo(5 * HEI);
        make.width.mas_equalTo(WIDTH - (20 * WID));
        make.height.equalTo(self.titleV).offset(-10);
        
    }];
    self.segmentC.tintColor = [UIColor lightGrayColor];
    self.segmentC.selectedSegmentIndex = 0;
    self.segmentC.layer.cornerRadius = 8 * WID;
    //自定义segment
    [self.segmentC setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:17 / 256.0 green:82 / 256.0 blue:1 alpha:1]} forState:UIControlStateSelected];
    [self.segmentC addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    
    //scrollView
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.contentSize = CGSizeMake(WIDTH * 2, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.contentOffset = CGPointMake(0, 0);
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.equalTo(self.titleV.mas_bottom);
        make.width.mas_equalTo(WIDTH);
        make.height.mas_equalTo(HEIGHT - (49 + 64 + self.titleV.frame.size.height) * HEI);
        
    }];
    
    
    
    
    self.foreignVC = [[ForeignViewController alloc] init];
    [self.scrollView addSubview:self.foreignVC.view];
    [self.foreignVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.mas_equalTo(0);
        make.width.equalTo(self.scrollView);
        make.height.equalTo(self.scrollView);
        
    }];
    [self addChildViewController:self.foreignVC];
    
    self.inlandVC = [[InlandViewController alloc] init];
    self.inlandVC.view.frame = CGRectMake(WIDTH, 0, WIDTH, self.scrollView.frame.size.height);
    [self.scrollView addSubview:self.inlandVC.view];
    [self.inlandVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.foreignVC.view.mas_right);
        make.top.mas_equalTo(0);
        make.width.equalTo(self.scrollView);
        make.height.equalTo(self.scrollView);
    }];
    [self addChildViewController:self.inlandVC];
    
    
}

-(void)rightButtonAction{
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}

/*************** 分段按钮方法 ******************/
-(void)segmentAction:(UISegmentedControl *)segment{
    self.scrollView.contentOffset = CGPointMake(self.segmentC.selectedSegmentIndex * WIDTH, 0);
}
/*************** 滑动停止触发的方法 **************/
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.segmentC.selectedSegmentIndex = scrollView.contentOffset.x / WIDTH;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
